package cn.com.hhrcw.personal.wxlogin.web;

import cn.com.hhrcw.entity.CmsContent;
import cn.com.hhrcw.personal.thirdLogin.util.ThirdLoginUtil;
import cn.com.hhrcw.personal.wxlogin.util.Constant;
import cn.com.hhrcw.personal.wxlogin.util.WxLoginTokenUtil;
import cn.com.hhrcw.personal.wxlogin.vo.WxLoginVO;
import cn.com.hhrcw.service.ICmsContentService;
import cn.com.hhrcw.service.IMemberThirdService;
import cn.com.hhrcw.thirdLogin.vo.ThridLoginVO;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 微信登录-PC端
 *
 * @author lixingxing
 * @since 2020-11-06
 */
@RestController
@Slf4j
@RequestMapping("/personal/wxLogin/pc")
@Validated
public class WxLoginPCController extends JeecgController<CmsContent, ICmsContentService> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IMemberThirdService memberThirdService;

    @Value("${spring.profiles.active}")
    private String profiles;

    /**
     * 生成登陆token(PC 端调用)
     *
     * @return
     */
    @PostMapping(value = "/setToken")
    public Result<?> setToken() {

        String token = WxLoginTokenUtil.createToken();
        //默认1分钟失效
        final long time = StrUtil.equals(profiles, "dev") ? (Constant.TIME_MIN * 24) : Constant.TIME_MIN;

        redisTemplate.opsForValue().set(token, cn.com.hhrcw.personal.wxlogin.util.Constant.WAIT, time, TimeUnit.MINUTES);

        log.info("profiles {} token:{},失效时间：{}分钟", profiles, token, time);
        return Result.ok(token);
    }

    /**
     * 检查情况
     *
     * @return
     */
    @GetMapping(value = "/check")
    public Result<WxLoginVO> check(String token) {//

        final String loginState = (String) redisTemplate.opsForValue().get(token);

        WxLoginVO vo = new WxLoginVO();
        vo.setLoginState(loginState);

        //取出第三方登录信息（微信昵称 等信息）
        final ThridLoginVO t = (ThridLoginVO) redisTemplate.opsForValue().get(WxLoginTokenUtil.getWxThirdObjToken(token));
        redisTemplate.delete(WxLoginTokenUtil.getWxThirdObjToken(token));

        if (StringUtils.isBlank(loginState)) {
            vo.setMessage("token 过期");
            return Result.ok(vo);

        }
        //OK登录
        else if (loginState.equalsIgnoreCase(cn.com.hhrcw.personal.wxlogin.util.Constant.OK)) {

            String thirdToken = ThirdLoginUtil.createToken();
            redisTemplate.opsForValue().set(thirdToken, t, 3, TimeUnit.MINUTES);
            vo.setThirdToken(thirdToken);
            vo.setMessage("绑定");
            return Result.ok(vo);

        }
        //未绑定
        else if (loginState.equalsIgnoreCase(Constant.UNBIND)) {
            if (null == t) {
                log.warn("未绑定,获取第三方登录信息为null WxLoginTokenUtil.getWxThirdObjToken(token)={}", WxLoginTokenUtil.getWxThirdObjToken(token));
                return Result.ok(vo);
            } else {
                String thirdToken = ThirdLoginUtil.createToken();
                //绑定登录时使用
                redisTemplate.opsForValue().set(thirdToken, t, 2, TimeUnit.HOURS);
                vo.setThirdToken(thirdToken);
                vo.setMessage("绑定");
                return Result.ok(vo);
            }

        } else {
            return Result.ok(vo);
        }

    }

}
